import {createRouter, createWebHashHistory} from 'vue-router'
import BasicLayout from "./layout/BasicLayout.vue";
import UserLayout from "@/layout/UserLayout.vue";
import {ApiGetConfig} from "@/api/system.js";

//路由数组
const routes = [{
    path: "/user", name: "user", meta: {hideInMenu: true}, children: [{
        path: 'login',
        name: 'login',
        meta: {hideInMenu: true},
        component: () => import(/* webpackChunkName: "user" */ './views/user/login.vue')
    }, {
        path: "register",
        name: "register",
        meta: {hideInMenu: true},
        component: () => import(/* webpackChunkName: "user" */ '@/views/user/register.vue')
    },],
}, {
    path: "/share",
    name: 'share',
    meta: {hideInMenu: true},
    component: () => import(/* webpackChunkName: "user" */ './views/toolkit/share.vue')
}

]

const dynamicRouter = [{
    path: '/',
    name: 'index',
    component: BasicLayout,
    meta: {title: '首页', hideInMenu: true},
    redirect: '/toolkit',
    title: '首页',
    children: [{
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {title: '图表', icon: 'pi-chart-bar',},
    }, {
        path: 'instance',
        name: 'instance',
        component: () => import('@/views/cmdb/instance.vue'),
        meta: {title: '资产数据', icon: 'pi-box', hideInMenu: true},
    }, {
        path: 'cmdb',
        name: 'cmdb',
        component: () => import('@/views/cmdb/schema.vue'),
        meta: {title: '资产', icon: 'pi-qrcode'},
    }, {
        path: 'toolkit',
        name: 'toolkit',
        title: '首页',
        component: () => import('@/views/toolkit/index.vue'),
        meta: {title: '工具库', icon: 'pi-box'},
    }, {
        path: 'FileLibrary',
        name: 'FileLibrary',
        component: () => import('@/views/FileLibrary/FileView.vue'),
        meta: {title: '文件库', icon: 'pi-folder', keepAlive: true}
    }, {
        path: 'scheduler',
        name: 'scheduler',
        component: () => import('@/views/scheduler/index.vue'),
        meta: {title: '定时任务', icon: 'pi-clock'}
    }, {
        path: 'service',
        name: 'service',
        component: () => import('@/views/service/index.vue'),
        meta: {title: '服务管理', icon: 'pi-bolt'}
    }, {
        path: 'configuration',
        name: 'configuration',
        component: () => import('@/views/system/configuration.vue'),
        meta: {title: '系统配置', icon: 'pi-cog'}
    }, {
        path: '/toolkit/preview',
        name: 'preview',
        component: () => import('@/views/toolkit/preview.vue'),
        meta: {title: '编辑', hideInMenu: true}
    }, {
        path: '/toolkit/history-log',
        name: 'log',
        component: () => import('@/views/toolkit/log.vue'),
        meta: {title: '日志', hideInMenu: true}
    },

        {
            path: 'task-histories',
            name: 'TaskHistories',
            component: () => import('@/views/scheduler/history.vue'),
            meta: {title: '任务执行历史', icon: 'pi-clock', hideInMenu: true}
        },

    ]
},]

export const syncRouter = async () => {
    const result = await ApiGetConfig();
    // 创建一个集合，以便快速查找
    const enabledRoutes = new Set(result.data.config.menus.filter(route => route.on).map(route => route.name));

    // 过滤路由
    const filteredRoutes = dynamicRouter.map(route => {
        return {
            ...route, children: route.children.filter(child => enabledRoutes.has(child.name))
        };
    });

    return filteredRoutes;
}


//路由对象
const router = createRouter({
    history: createWebHashHistory(),//has模式
    routes //上面的路由数组

})


//导出路由对象，在main.js中引用
export default router
